package com.gdth.widget.wheel;

import java.util.HashMap;
import java.util.Map;

import com.gdth.R;
import com.gdth.widget.dialog.BaseDialog;
import com.gdth.widget.wheel.adapter.ArrayWheelAdapter;
import com.gdth.widget.wheel.adapter.WheelAdapter;
import com.gdth.widget.wheel.listener.OnWheelChangedListener;

import android.content.Context;    
import android.view.View;  
import android.widget.TextView;

/** 
 * @author Arvin
 * @version 2015年6月10日 下午4:16:53 
 * 
 */
public class WheelViewLayout extends BaseDialog{
	/**
	 * 确定事件
	 */
	private View.OnClickListener mOnClickListener;
	
	/** 
     * WheelView
     */  
    private WheelView wheelOne, wheelTwo;
    
    /** 
     * WheelView
     */  
    private Map<String, String[]> twoDatasMap = new HashMap<String, String[]>();  

    /**
     * WheelView
     */  
    private String sCurrentOne; 
    public int pCurrent;
    
    /** 
     * WheelView
     */  
    public String sCurrentTwo ="";
    
    public int station = 0;
    
    public final static int RESULT_CODE=1;
	
	public WheelViewLayout(Context context) {  
        super(context, R.style.dialog_theme);  
    }  
  
    public WheelViewLayout(Context context, String title, View.OnClickListener mOnClickListener) {  
        super(context, R.style.dialog_theme); 
        
        this.title = title;
        this.mOnClickListener = mOnClickListener;
        
        initView();
    }  
	  
    public void initView(){
    	super.initView(R.layout.wheelview_two);
    	
    	TextView txt_title = (TextView) findViewById(R.id.dialog_title);
		txt_title.setText(title);
		
		TextView txt_config = (TextView) findViewById(R.id.dialog_button_ok);
		
		if (mOnClickListener != null)
			txt_config.setOnClickListener(mOnClickListener);
		
		TextView txt_cancle = (TextView) findViewById(R.id.dialog_button_cancel);
		txt_cancle.setOnClickListener(new dismissOnClickListener());
		
		wheelOne = (WheelView) findViewById(R.id.wheel_view_one);  
        wheelTwo = (WheelView) findViewById(R.id.wheel_view_two);  
        
        wheelOne.setViewAdapter(new ArrayWheelAdapter<String>(this.context, null));
        
        wheelOne.addChangingListener(onWheelChange);
        wheelTwo.addChangingListener(onWheelChange); 
  
        wheelOne.setVisibleItems(1);
        wheelTwo.setVisibleItems(1); 
        updateWheelTwo();  
    }  
        
    /** 
     * 更新第二个Wheel的数据
     */  
    private void updateWheelTwo(){  
        pCurrent = wheelOne.getCurrentItem();  
        sCurrentOne = null;
        String[] areas = twoDatasMap.get(sCurrentOne);  
        
        sCurrentTwo = twoDatasMap.get(sCurrentOne)[0];  
        
        if (areas == null){  
            areas = new String[] { "" };  
        }  
        wheelTwo.setViewAdapter(new WheelAdapter<String>(this.context, areas));  
        wheelTwo.setCurrentItem(0);  
    }  
    
	private OnWheelChangedListener onWheelChange = new OnWheelChangedListener(){
		@Override
		public void onChanged(WheelView wheel, int oldValue, int newValue) {
			// TODO Auto-generated method stub
			if (wheel == wheelOne){
            	updateWheelTwo();
            } 
			else if (wheel == wheelTwo)  {  
            	sCurrentTwo = twoDatasMap.get(sCurrentOne)[newValue]; 
            }  
		}
		
	};
}
